package scau.gsmg.misp.action;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import scau.gsmg.misp.service.IMessageService;

import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("json-default")
@Scope("prototype")
@Namespace(value = "/")

@Results({
	@Result(name = "success", type = "json", params = { "root", "root" }),
	@Result(name = "jsonObj", type = "json", params = { "root",
	"jsonObj" }) 
	})
@Controller
public class MessageAction extends ActionSupport{

	private int id;
	private String password;
	private JSONObject jsonObj;
	@Resource
	private IMessageService messageService;
	@Action(value="searcRoles")
	public String searcRoles(){
		
		String roles = messageService.searcRoles(id);
		JSONObject data = new JSONObject();
		if (!"".equals(roles)) {
			data.put("success", true);
			data.put("roles",roles);
		} else {
			data.put("success", false);
		}
		this.jsonObj = data;
		System.out.println("searcRoles"+data.toString());
		return "jsonObj";
	}
	
	@Action(value="searchPasswd")
	public String searchPasswd(){
		boolean permission = messageService.searchPasswd(id, password);
		JSONObject data = new JSONObject();
		if (permission) {
			data.put("success", true);
			data.put("message", "密码正确！");
		} else {
			data.put("success", false);
			data.put("message", "密码错误！");
		}
		this.jsonObj = data;
		return "jsonObj";
	}
	
	@Action(value="updatePasswd")
	public String updatePasswd(){
		boolean permission = messageService.updatePasswd(id, password);
		JSONObject data = new JSONObject();
		if (permission) {
			data.put("success", true);
			data.put("message", "密码更新成功！");
		} else {
			data.put("success", false);
			data.put("message", "密码更新失败！");
		}
		this.jsonObj = data;
		return "jsonObj";
	}

	
	public IMessageService getMessageService() {
		return messageService;
	}

	public void setMessageService(IMessageService messageService) {
		this.messageService = messageService;
	}

	public JSONObject getJsonObj() {
		return jsonObj;
	}

	public void setJsonObj(JSONObject jsonObj) {
		this.jsonObj = jsonObj;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	
}
